package com.itheima.reggie.handler;


import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.exception.CustomException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

//全局异常处理
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {


    @ExceptionHandler(Exception.class)
    public ResultInfo handlerException(Exception e) {
        //打印日志
        e.printStackTrace();
        if (e.getMessage().contains("idx_category_name")) {
            return ResultInfo.error("分类名称重复");
        }
        return ResultInfo.error("服务器开小差了，请稍后再试");
    }

    @ExceptionHandler(CustomException.class)
    public ResultInfo handlerCustomException(Exception e){
        //打印异常信息
        e.printStackTrace();
        return ResultInfo.error(e.getMessage());
    }
}